package com.zp.self.filter;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
public class HostAddrKeyResolver implements KeyResolver {
    //1.KeyResolver需要实现resolve方法，比如根据Hostname进行限流，则需要用hostAddress去判断
    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        return Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
    }
    
    //2.可以根据uri去限流，这时KeyResolver代码如下：
//    @Override
//    public Mono<String> resolve(ServerWebExchange exchange) {
//        return Mono.just(exchange.getRequest().getURI().getPath());
//    }
    
    //3也可以以用户的维度去限流：lamda而已  写在注册入 HostAddrKeyResolver  方法理
//     return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
}